﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace net.xBei.Models {
    /// <summary>
    /// 自动释放
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class AutoDisposableData<TData> {
        private readonly TData data;
        private readonly DateTime expires;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="expires">过期时间</param>
        public AutoDisposableData(TData data, DateTime expires) {
            this.data = data;
            this.expires = expires;
        }
        /// <summary>
        /// 数据
        /// </summary>
        public TData? Value => expires > DateTime.Now ? data : default;
        /// <summary>
        /// 是否过期
        /// </summary>
        public bool IsExpired => expires <= DateTime.Now;
    }
}
